package cipher.util;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES implements IFCrypt{
    Cipher ecipher;
    Cipher dcipher;

    public  AES(String pwd) {
        try {
        	SecretKey key = new SecretKeySpec(getEnKey(pwd), "AES");
            byte[] salt = {
                    (byte)0xC7, (byte)0x73, (byte)0x21, (byte)0x8C,
                    (byte)0x7E, (byte)0xC8, (byte)0xEE, (byte)0x99,
                    (byte)0xC7, (byte)0x73, (byte)0x21, (byte)0x8C,
                    (byte)0x7E, (byte)0xC8, (byte)0xEE, (byte)0x99};
    
            IvParameterSpec iv;
            iv = new javax.crypto.spec.IvParameterSpec(salt);
    
            ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            ecipher.init(Cipher.ENCRYPT_MODE, key,iv);
            dcipher.init(Cipher.DECRYPT_MODE, key,iv);
    
        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (Exception e) {
        	e.printStackTrace();
        } 
    }
    
    public byte[] encrypt( byte [] str, int pos, int off) {
        try {
            byte[] enc = ecipher.doFinal(str,pos,off);
            
            return enc;
        } catch (Exception e) {
        	try {
        		ecipher.doFinal();
        	} catch (Exception e1) {
				e1.printStackTrace();
			}
        } 
        return null;
    }
    
    public int encrypt( byte [] str, int pos, int off , byte [] out) {
        try {
            return  ecipher.doFinal(str,pos,off,out);
        } catch (Exception e) {
        	try {
        		ecipher.doFinal();
        	} catch (Exception e1) {
				e1.printStackTrace();
			}
        }
        return 0;
    }
    
    public byte[] decrypt( byte [] str, int pos ,int off) {
        try {
            byte[] utf8 = dcipher.doFinal(str,pos,off);
            
            return utf8;
        } catch (Exception e) {
        	try {
				dcipher.doFinal();
        	} catch (Exception e1) {
				e1.printStackTrace();
			}
        } 
        return null;
    }
    public int decrypt( byte [] str, int pos ,int off, byte [] rtn) {
        int c = 0;
    	try {
        	c = dcipher.doFinal(str, pos, off, rtn);
            return c;
        } catch (Exception e) {
        	try {
				dcipher.doFinal();
        	} catch (Exception e1) {
				e1.printStackTrace();
			}
        } 
        return 0;
    }
    public int decrypt( byte [] str, int pos ,int off, byte [] rtn,int rtnoff) {
        int c = 0;
    	try {
        	c = dcipher.doFinal(str, pos, off, rtn,rtnoff);
            return c;
        } catch (Exception e) {
        	try {
				dcipher.doFinal();
        	} catch (Exception e1) {
				e1.printStackTrace();
			}
        } 
        return 0;
    }
        
    private byte[] getEnKey(String spKey)
    {
    	byte[] desKey=null;
    	try
    	{
    	  byte[] desKey1 = md5Encrypt(spKey);
    	  desKey = new byte[16];
    	  int i = 0;
    	  while (i < desKey1.length && i < 16) {
    		desKey[i] = desKey1[i];
    		i++;
    	  }
    	  if (i < 16) {         
    		desKey[i] = 0;
    		i++;
    	  }
    	}
    	catch(Exception e){
    	  e.printStackTrace();
    	}
    	  	
    	return desKey;
    }
    
    private byte[] md5Encrypt(String strSrc)
    {
    	byte[] returnByte = null;
    	try
    	{
    		MessageDigest md5 = MessageDigest.getInstance("MD5"); 
    		returnByte = md5.digest(strSrc.getBytes());
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	return returnByte;
    }  
       
}
